<?php
App::uses('AppController', 'Controller');
/**
 * Tamungs Controller
 *
 * @property Tamung $Tamung
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class TamungsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator', 'Session');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Tamung->recursive = 0;
		$this->set('tamungs', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Tamung->exists($id)) {
			throw new NotFoundException(__('Invalid tamung'));
		}
		$options = array('conditions' => array('Tamung.' . $this->Tamung->primaryKey => $id));
		$this->set('tamung', $this->Tamung->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add($id = null,$slug = null) {
		//debug($this->request->data);
		$id = $this->params['id'];
		$this->set('saved', false);
		$data = array();
		if ($this->request->is('post')) {
			$price = str_replace(',', '', $this->request->data['Tamung']['money']);
			$this->loadModel('Salary');
			$salary = $this->Salary->find('first',array('conditions'=>array('Salary.users_id'=>$id,'Salary.traluong'=>0)));
			if($salary != null){
				$tamung = $price + $salary['Salary']['tamung'];
				//debug($phucap);
				$this->Tamung->query("UPDATE  salaries set tamung='".$tamung."' where users_id= '".$id."';");
			}else{
				$tamung = $price;
				$this->Tamung->query("INSERT INTO  salaries(users_id,tamung) value ('".$id."','".$tamung."');");
			}
			$id_salary = $this->Salary->find('first',array('fields'=>array('id'),'conditions'=>array('Salary.users_id'=>$id,'Salary.traluong'=>0)));			
			$data['Tamung']['money'] = $price;
			$data['Tamung']['salaries_id'] = $id_salary['Salary']['id'];
			$data['Tamung']['reason'] = $this->request->data['Tamung']['reason'];
			$data['Tamung']['ngayung'] = date("Y-m-d");
			$data['Tamung']['users_id'] = $id;
			//debug($data);
			$this->Tamung->create();				
			if ($this->Tamung->save($data)) {
				$this->set('saved', true);
			} else {
				//$this->Session->setFlash(__('The phu cap could not be saved. Please, try again.'));
			}			
		}
		$users = $this->Tamung->User->find('first',array('conditions'=>array('User.id'=>$id)));
		$this->set(compact('users'));		
	}
/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Tamung->exists($id)) {
			throw new NotFoundException(__('Invalid tamung'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Tamung->save($this->request->data)) {
				$this->Session->setFlash(__('The tamung has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The tamung could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Tamung.' . $this->Tamung->primaryKey => $id));
			$this->request->data = $this->Tamung->find('first', $options);
		}
		$users = $this->Tamung->User->find('list');
		$salaries = $this->Tamung->Salary->find('list');
		$this->set(compact('users', 'salaries'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Tamung->id = $id;
		if (!$this->Tamung->exists()) {
			throw new NotFoundException(__('Invalid tamung'));
		}
		$this->request->allowMethod('post', 'delete');
		if ($this->Tamung->delete()) {
			$this->Session->setFlash(__('The tamung has been deleted.'));
		} else {
			$this->Session->setFlash(__('The tamung could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}
}
